package com.loe.picker.util;

import android.graphics.Typeface;
import android.view.View;

import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnItemSelectedListener;

import java.util.List;

public class WheelNetOptions
{
    private View view;
    private WheelView wv_option1;
    private WheelView wv_option2;
    private WheelView wv_option3;

    public List<PickerBean> mOptions1Items;
    public List<PickerBean> mOptions2Items;
    public List<PickerBean> mOptions3Items;
    private OnItemSelectedListener wheelListener_option1;
    private OnItemSelectedListener wheelListener_option2;

    //文字的颜色和分割线的颜色
    int textColorOut;
    int textColorCenter;
    int dividerColor;

    private WheelView.DividerType dividerType;

    // 条目间距倍数
    float lineSpacingMultiplier = 1.6F;

    private int index1 = 0;

    public View getView()
    {
        return view;
    }

    public void setView(View view)
    {
        this.view = view;
    }

    public WheelNetOptions(final OptionsPickerNetView tOptionsPickerNetView, View view)
    {
        super();
        this.view = view;
        wv_option1 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options1);// 初始化时显示的数据
        wv_option2 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options2);
        wv_option3 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options3);

        wv_option1.setIsOptions(true);
        wv_option2.setIsOptions(true);
        wv_option3.setIsOptions(true);

        // 联动监听器
        wheelListener_option1 = new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(int index)
            {
                if(index>=0)
                {
                    index1 = index;
                    tOptionsPickerNetView.loadList2(index,mOptions1Items.get(index), new OnLoadOkListener()
                    {
                        @Override
                        public void onLoadOk(List list)
                        {
                            mOptions2Items = list;
                            wv_option2.setAdapter(new CodeWheelAdapter(list));
                            wv_option2.setCurrentItem(0);
                            wheelListener_option2.onItemSelected(0);
                        }
                    });
                }
            }
        };
        wheelListener_option2 = new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(int index)
            {
                if(index>=0)
                {
                    tOptionsPickerNetView.loadList3(index1,index,mOptions2Items.get(index), new OnLoadOkListener()
                    {
                        @Override
                        public void onLoadOk(List list)
                        {
                            mOptions3Items = list;
                            wv_option3.setAdapter(new CodeWheelAdapter(list));
                            wv_option3.setCurrentItem(0);
                        }
                    });
                }
            }
        };

        // 添加联动监听
        wv_option1.setOnItemSelectedListener(wheelListener_option1);
        wv_option2.setOnItemSelectedListener(wheelListener_option2);
    }

    //不联动情况下
    public void setPicker(List<PickerBean> options1Items, List<PickerBean> options2Items, List<PickerBean> options3Items)
    {
        this.mOptions1Items = options1Items;
        this.mOptions2Items = options2Items;
        this.mOptions3Items = options3Items;
        int len = CodeWheelAdapter.DEFAULT_LENGTH;
        if (this.mOptions3Items == null)
        {
            len = 8;
        }
        if (this.mOptions2Items == null)
        {
            len = 12;
        }
        // 选项1
        wv_option1.setAdapter(new CodeWheelAdapter(mOptions1Items, len));// 设置显示数据
        wv_option1.setCurrentItem(0);// 初始化时显示的数据
        // 选项2
        if (mOptions2Items != null)
        {
            wv_option2.setAdapter(new CodeWheelAdapter(mOptions2Items));// 设置显示数据
        }
        wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据
        // 选项3
        if (mOptions3Items != null)
        {
            wv_option3.setAdapter(new CodeWheelAdapter(mOptions3Items));// 设置显示数据
        }
        wv_option3.setCurrentItem(wv_option3.getCurrentItem());
        wv_option1.setIsOptions(true);
        wv_option2.setIsOptions(true);
        wv_option3.setIsOptions(true);

        if (this.mOptions2Items == null)
        {
            wv_option2.setVisibility(View.GONE);
        }
        else
        {
            wv_option2.setVisibility(View.VISIBLE);
        }
        if (this.mOptions3Items == null)
        {
            wv_option3.setVisibility(View.GONE);
        }
        else
        {
            wv_option3.setVisibility(View.VISIBLE);
        }
        /////////////////////////////////////////////
        wheelListener_option1.onItemSelected(0);
    }


    public void setTextContentSize(int textSize)
    {
        wv_option1.setTextSize(textSize);
        wv_option2.setTextSize(textSize);
        wv_option3.setTextSize(textSize);
    }

    private void setTextColorOut()
    {
        wv_option1.setTextColorOut(textColorOut);
        wv_option2.setTextColorOut(textColorOut);
        wv_option3.setTextColorOut(textColorOut);

    }

    private void setTextColorCenter()
    {
        wv_option1.setTextColorCenter(textColorCenter);
        wv_option2.setTextColorCenter(textColorCenter);
        wv_option3.setTextColorCenter(textColorCenter);

    }

    private void setDividerColor()
    {
        wv_option1.setDividerColor(dividerColor);
        wv_option2.setDividerColor(dividerColor);
        wv_option3.setDividerColor(dividerColor);
    }

    private void setDividerType()
    {
        wv_option1.setDividerType(dividerType);
        wv_option2.setDividerType(dividerType);
        wv_option3.setDividerType(dividerType);
    }

    private void setLineSpacingMultiplier()
    {
        wv_option1.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_option2.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_option3.setLineSpacingMultiplier(lineSpacingMultiplier);

    }

    /**
     * 设置选项的单位
     *
     * @param label1 单位
     * @param label2 单位
     * @param label3 单位
     */
    public void setLabels(String label1, String label2, String label3)
    {
        if (label1 != null)
        {
            wv_option1.setLabel(label1);
        }
        if (label2 != null)
        {
            wv_option2.setLabel(label2);
        }
        if (label3 != null)
        {
            wv_option3.setLabel(label3);
        }
    }

    /**
     * 设置x轴偏移量
     */
    public void setTextXOffset(int xoffset_one, int xoffset_two, int xoffset_three)
    {
        wv_option1.setTextXOffset(xoffset_one);
        wv_option2.setTextXOffset(xoffset_two);
        wv_option3.setTextXOffset(xoffset_three);
    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic 是否循环
     */
    public void setCyclic(boolean cyclic)
    {
        wv_option1.setCyclic(cyclic);
        wv_option2.setCyclic(cyclic);
        wv_option3.setCyclic(cyclic);
    }

    /**
     * 设置字体样式
     *
     * @param font 系统提供的几种样式
     */
    public void setTypeface(Typeface font)
    {
        wv_option1.setTypeface(font);
        wv_option2.setTypeface(font);
        wv_option3.setTypeface(font);
    }

    /**
     * 分别设置第一二三级是否循环滚动
     *
     * @param cyclic1,cyclic2,cyclic3 是否循环
     */
    public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3)
    {
        wv_option1.setCyclic(cyclic1);
        wv_option2.setCyclic(cyclic2);
        wv_option3.setCyclic(cyclic3);
    }


    /**
     * 返回当前选中的结果对应的位置数组 因为支持三级联动效果，分三个级别索引，0，1，2。
     * 在快速滑动未停止时，点击确定按钮，会进行判断，如果匹配数据越界，则设为0，防止index出错导致崩溃。
     *
     * @return 索引数组
     */
    public int[] getCurrentItems()
    {
        int[] currentItems = new int[3];
        currentItems[0] = wv_option1.getCurrentItem();
        currentItems[1] = wv_option2.getCurrentItem();
        currentItems[2] = wv_option3.getCurrentItem();
        return currentItems;
    }

    public void setCurrentItems(int option1, int option2, int option3)
    {

        wv_option1.setCurrentItem(option1);
        wv_option2.setCurrentItem(option2);
        wv_option3.setCurrentItem(option3);
    }

    /**
     * 设置间距倍数,但是只能在1.2-2.0f之间
     *
     * @param lineSpacingMultiplier
     */
    public void setLineSpacingMultiplier(float lineSpacingMultiplier)
    {
        this.lineSpacingMultiplier = lineSpacingMultiplier;
        setLineSpacingMultiplier();
    }

    /**
     * 设置分割线的颜色
     *
     * @param dividerColor
     */
    public void setDividerColor(int dividerColor)
    {
        this.dividerColor = dividerColor;
        setDividerColor();
    }

    /**
     * 设置分割线的类型
     *
     * @param dividerType
     */
    public void setDividerType(WheelView.DividerType dividerType)
    {
        this.dividerType = dividerType;
        setDividerType();
    }

    /**
     * 设置分割线之间的文字的颜色
     *
     * @param textColorCenter
     */
    public void setTextColorCenter(int textColorCenter)
    {
        this.textColorCenter = textColorCenter;
        setTextColorCenter();
    }

    /**
     * 设置分割线以外文字的颜色
     *
     * @param textColorOut
     */
    public void setTextColorOut(int textColorOut)
    {
        this.textColorOut = textColorOut;
        setTextColorOut();
    }

    /**
     * Label 是否只显示中间选中项的
     *
     * @param isCenterLabel
     */

    public void isCenterLabel(Boolean isCenterLabel)
    {
        wv_option1.isCenterLabel(isCenterLabel);
        wv_option2.isCenterLabel(isCenterLabel);
        wv_option3.isCenterLabel(isCenterLabel);
    }

}

